@echo off

if [%1]==[justcompile] goto :justcompile

echo Generating pals
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts0.png out=work\palts0.h label=palts0 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts1.png out=work\palts1.h label=palts1 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts2.png out=work\palts2.h label=palts2 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts2a.png out=work\palts2a.h label=palts2a silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts3.png out=work\palts3.h label=palts3 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts3a.png out=work\palts3a.h label=palts3a silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts4.png out=work\palts4.h label=palts4 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts4a.png out=work\palts4a.h label=palts4a silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts5.png out=work\palts5.h label=palts5 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts5a.png out=work\palts5a.h label=palts5a silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts5b.png out=work\palts5b.h label=palts5b silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palts5c.png out=work\palts5c.h label=palts5c silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palss0.png out=work\palss0.h label=palss0 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palss0a.png out=work\palss0a.h label=palss0a silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palss1.png out=work\palss1.h label=palss1 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palss2.png out=work\palss2.h label=palss2 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palcuts0.png out=work\palcuts0.h label=palcuts0 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palcuts1.png out=work\palcuts1.h label=palcuts1 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\palcuts2.png out=work\palcuts2.h label=palcuts2 silent
..\..\..\src\utils\mkts.exe mode=pals pals=..\gfx\paltstitle.png out=work\paltstitle.h label=paltstitle silent
copy /b work\pal*.h assets\palettes.h > nul

rem chr-rom mapping
rem CHRROM0: 8192 generated by import_patterns0.spt
rem CHRROM1: 4096 generated by import_patterns1.spt + enem data levels 0, 1, 2, 3, 4.
rem CHRROM2: 8192 generated by map exporter, continues on...
rem CHRROM3: ?    generated by map exporter, + 2048 enem data level 5

echo Exporting chr
cd ..\gfx
..\..\..\src\utils\mkts.exe mode=scripted in=import_patterns0.spt out=..\dev\tileset0.chr silent
..\..\..\src\utils\mkts.exe mode=scripted in=import_patterns1.spt out=..\dev\work\tileset1p.bin silent

echo Generating rle'd screens
..\..\..\src\utils\namgen.exe in=cuts0.png out=..\dev\assets\cuts0_rle.h pals=palcuts0.png chr=..\dev\tileset1.chr rle=cuts0_rle
..\..\..\src\utils\namgen.exe in=cuts1.png out=..\dev\assets\cuts1_rle.h pals=palcuts1.png chr=..\dev\tileset1.chr rle=cuts1_rle
..\..\..\src\utils\namgen.exe in=cuts2.png out=..\dev\assets\cuts2_rle.h pals=palcuts2.png chr=..\dev\tileset1.chr rle=cuts2_rle
..\..\..\src\utils\namgen.exe in=title.png out=..\dev\assets\title_rle.h pals=paltstitle.png chr=..\dev\tileset1.chr rle=title_rle

echo Exporting enems
cd ..\enems
..\..\..\src\utils\eneexp3.exe level00.ene ..\dev\work\enems00.h 00 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level01.ene ..\dev\work\enems01.h 01 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level02.ene ..\dev\work\enems02.h 02 1 gencounter bin

..\..\..\src\utils\eneexp3.exe level10.ene ..\dev\work\enems10.h 10 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level11.ene ..\dev\work\enems11.h 11 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level12.ene ..\dev\work\enems12.h 12 1 gencounter bin

..\..\..\src\utils\eneexp3.exe level20.ene ..\dev\work\enems20.h 20 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level21.ene ..\dev\work\enems21.h 21 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level22.ene ..\dev\work\enems22.h 22 1 gencounter bin

..\..\..\src\utils\eneexp3.exe level30.ene ..\dev\work\enems30.h 30 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level31.ene ..\dev\work\enems31.h 31 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level32.ene ..\dev\work\enems32.h 32 1 gencounter bin

..\..\..\src\utils\eneexp3.exe level40.ene ..\dev\work\enems40.h 40 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level41.ene ..\dev\work\enems41.h 41 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level42.ene ..\dev\work\enems42.h 42 1 gencounter bin

..\..\..\src\utils\eneexp3.exe level50.ene ..\dev\work\enems50.h 50 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level51.ene ..\dev\work\enems51.h 51 1 gencounter bin
..\..\..\src\utils\eneexp3.exe level52.ene ..\dev\work\enems52.h 52 1 gencounter bin

cd ..\dev
copy /b work\enems*.h assets\enem_constants.h > nul
..\..\..\src\utils\binpaster.exe index=work\enem_index1.h out=work\enems1.bin files=work\enems00.h.bin,work\enems01.h.bin,work\enems02.h.bin,work\enems10.h.bin,work\enems11.h.bin,work\enems12.h.bin,work\enems20.h.bin,work\enems21.h.bin,work\enems22.h.bin,work\enems30.h.bin,work\enems31.h.bin,work\enems32.h.bin,work\enems40.h.bin,work\enems41.h.bin,work\enems42.h.bin
..\..\..\src\utils\binpaster.exe index=work\enem_index2.h out=work\enems2.bin files=work\enems50.h.bin,work\enems51.h.bin,work\enems52.h.bin
copy /b work\enem_index1.h + work\enem_index2.h assets\enem_index.h

echo Compiling enembehs
cd ..\script
..\..\..\src\utils\pencompiler.exe enembehs.spt ..\dev\assets\compiled_enems.h debug

echo Making map
cd ..\map
..\..\..\src\utils\rle44mapchrrom.exe in=maplist.txt bin=..\dev\work\mapchr.bin out=..\dev\assets\chr_rom_maps.h chr=2
cd ..\dev

echo Resizing and pasting binaries into CHR-ROM images

..\..\..\src\utils\fillto.exe work\mapchr.bin.3 6144
..\..\..\src\utils\fillto.exe work\enems1.bin 4096
..\..\..\src\utils\fillto.exe work\enems2.bin 2048

copy /b work\tileset1p.bin + work\enems1.bin tileset1.chr
copy work\mapchr.bin.2 tileset2.chr
copy /b work\mapchr.bin.3 + work\enems2.bin tileset3.chr

echo Exporting music and sound
cd ..\dev
..\..\..\src\utils\text2data.exe ..\ogt\music.txt -ca65 -ch1..4
..\..\..\src\utils\nsf2data.exe ..\ogt\sounds.nsf -ca65 -ntsc
copy ..\ogt\music.s > nul
copy ..\ogt\sounds.s > nul

cd ..\dev

if [%1]==[nocompile] goto :end

:justcompile
if [%2]==[noscript] goto :noscript

echo Building script
cd ..\script
..\..\..\src\utils\mscmk1.exe script.spt ..\dev\assets\mscnes.h 25
cd ..\dev

:noscript
cc65 -Oi game.c --add-source -D CNROM
ca65 crt0.s -o crt0.o -D CNROM=1
ca65 game.s
ld65 -v -C nes-CNROM.cfg -o cart.nes crt0.o game.o runtime.lib -m labels.txt -vm

del *.o > nul
rem del game.s > nul

copy cart.nes ..\..\espitene.nes
del work\*.h /q > nul
del work\*.bin.* /q > nul  2> nul

echo DONE!
